/**
 * 
 */
package org.cuatrovientos.vehiculo;

import java.util.Vector;





/**
 * @author teodor_mivovan
 *
 */
public class Race {

	private String name;
	private Track track;
	private Vector<Car> contestant = new Vector<Car>();
	
	/**
	 * @param name
	 * @param track
	 */
	public Race(String name, Track track) {
		this.name = name;
		this.track = track;
	}
	/**
	 * Adds a vehicle to the vector
	 * @param v
	 */
	public void insertCar(Car v){
		contestant.add(v);
	}
	/**
	 * Method that calculates the time it takes to the car to complete the race
	 * @param v
	 * @return
	 */
	private int timer(Car v){
         int seconds =0;
         int km=0;
         while (km < track.getKilometres()){
                 seconds++;
                 km+=v.move();
         }
         return seconds;
	 }
	/**
	 * Calculates which vehicle is the fastest.
	 * @return
	 */
	public Car Run(){
		int counter2=0;
		int counter=0;
		int aux=0;
				
		counter=1000;
        for (int i=1;i< contestant.size();i++){
               counter2=timer(contestant.get(i));
                if (counter2<counter){
                       aux=i;
                       counter=counter2;
               }
        }
        return contestant.get(aux);	
		}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	}
	
	
	

